package webservices;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import classes.Artikal;
import classes.Stavka;
import dao.DAOFactory;

@Path("/stavke")
public class StavkaResource {
	@Context
    UriInfo uriInfo;
    @Context
    Request request;
 
 
	@Path("/svi")
	@GET
	@Produces(MediaType.APPLICATION_XML)
	public GenericEntity<List<Stavka>> vratiStavke() {
		try {
			DAOFactory.connect();
			List<Stavka> stavke = DAOFactory.getStavkaDAO().selectSveStavke();
			DAOFactory.disconnect();
			return new GenericEntity<List<Stavka>>(stavke){};
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@POST
	@Path("stavka/obrisi")
	@Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public String obrisiStavku(@HeaderParam("username")String username, @HeaderParam("password")String password, String idStavka){
		try{
			int id = Integer.parseInt(idStavka);
		  	DAOFactory.connect();
		  	int i = DAOFactory.getStavkaDAO().deleteStavka(id);
		  	if (i!=1){
		  		DAOFactory.disconnect();
		  		return Boolean.toString(false);
		  	}
		  	DAOFactory.disconnect();
		  	return Boolean.toString(true);
		}
		catch(Exception e){
			e.printStackTrace();
			return Boolean.toString(false);
		}
	}
	
	@PUT
	@Path("stavka/dodaj")
	@Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public int dodajStavku(@HeaderParam("username")String username, @HeaderParam("password")String password, Stavka s){
		try{
			DAOFactory.connect();
		  	int i = DAOFactory.getStavkaDAO().insertStavka(s);
		  	if (i!=1){
		  		DAOFactory.disconnect();
		  		return 0;
		  	}
		  	DAOFactory.disconnect();
		  	return 1;
		}
		catch(Exception e){
			e.printStackTrace();
			return 0;
		}
	}
	
}
